﻿'HIDEasy
'Copyright (C) 2011 ASFX

'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 2
'of the License, or (at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program; if not, write to the Free Software
'Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Imports System.ComponentModel

<CLSCompliant(False)> _
Public Class HIDLinkCollectionNodeCollection
    Implements IList
    Implements IList(Of HIDLinkCollectionNode)
    Implements IDisposable


#Region "Variables"
    Private _List As List(Of HIDLinkCollectionNode) = New List(Of HIDLinkCollectionNode)
    Private _Parent As HIDDevice = Nothing
    Private _LockObject As New Object
#End Region

#Region "Creator"
    Friend Sub New()

    End Sub
#End Region
#Region "Properties"

#End Region
#Region "IList"

    Friend Overridable Sub Insert(ByVal index As Integer, _
                                  ByVal item As HIDLinkCollectionNode) Implements IList(Of HIDLinkCollectionNode).Insert
        SyncLock Me.SyncRoot
            _List.Insert(index, item)
        End SyncLock
    End Sub

    Default Public Property Item(ByVal index As Integer) As HIDLinkCollectionNode Implements IList(Of HIDLinkCollectionNode).Item
        Get
            Return _List(index)
        End Get
        Friend Set(ByVal value As HIDLinkCollectionNode)
            SyncLock Me.SyncRoot
                _List(index) = value
            End SyncLock
        End Set
    End Property

    Friend Shadows Sub RemoveAt(ByVal index As Integer) Implements IList(Of HIDLinkCollectionNode).RemoveAt
        Dim _item As HIDLinkCollectionNode = Me.Item(index)
        SyncLock Me.SyncRoot
            _List.RemoveAt(index)
        End SyncLock
    End Sub

    Public Function IndexOf(ByVal item As HIDLinkCollectionNode) As Integer Implements IList(Of HIDLinkCollectionNode).IndexOf
        Return _List.IndexOf(item)
    End Function

#End Region

#Region "ICollection"

    Friend Sub Add(ByVal item As HIDLinkCollectionNode) Implements ICollection(Of HIDLinkCollectionNode).Add
        SyncLock Me.SyncRoot
                _List.Add(item)
                item.Parent = Me
        End SyncLock
    End Sub

    Friend Sub AddRange(ByVal list As IEnumerable(Of HIDLinkCollectionNode))
        SyncLock Me.SyncRoot
            For Each _itm As HIDLinkCollectionNode In list
                Me.Add(_itm)
            Next
        End SyncLock
    End Sub

    Friend Function Remove(ByVal item As HIDLinkCollectionNode) As Boolean Implements ICollection(Of HIDLinkCollectionNode).Remove
        SyncLock Me.SyncRoot
            If Me.Contains(item) Then
                _List.Remove(item)
                item.Parent = Nothing
                Return True
            Else
                Return False
            End If
        End SyncLock
    End Function

    Friend Shadows Sub Clear() Implements ICollection(Of HIDLinkCollectionNode).Clear
        Do While Me.Count > 0
            Dim _Item As HIDLinkCollectionNode = Me.Item(0)
            Me.RemoveAt(0)
            _Item.Dispose()
        Loop
        _List.Clear()
    End Sub

    Public Function Contains(ByVal item As HIDLinkCollectionNode) As Boolean Implements ICollection(Of HIDLinkCollectionNode).Contains
        Return _List.Contains(item)
    End Function

    Public Overridable Sub CopyTo(ByVal array() As HIDLinkCollectionNode, _
                                  ByVal arrayIndex As Integer) Implements ICollection(Of HIDLinkCollectionNode).CopyTo
        SyncLock Me.SyncRoot
            _List.CopyTo(array, arrayIndex)
        End SyncLock
    End Sub

    <Category("Items")> _
    Public Shadows ReadOnly Property Count() As Integer Implements ICollection(Of HIDLinkCollectionNode).Count
        Get
            Return _List.Count
        End Get
    End Property

    Public ReadOnly Property IsReadOnly() As Boolean Implements ICollection(Of HIDLinkCollectionNode).IsReadOnly
        Get
            Return True
        End Get
    End Property

#End Region

#Region "IEnumerator"

    Public Function GetEnumerator() As IEnumerator(Of HIDLinkCollectionNode) Implements IEnumerable(Of HIDLinkCollectionNode).GetEnumerator
        Return New HIDLinkCollectionNodeEnumerator(_List.GetEnumerator)
    End Function

    Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
        Return New HIDLinkCollectionNodeEnumerator(_List.GetEnumerator)
    End Function

#End Region

    Public Overrides Function ToString() As String
        Select Case Me.Count
            Case 0
                Return "No Item"
            Case 1
                Return "1 Item"
            Case Else
                Return Me.Count.ToString & " Items"
        End Select
    End Function


#Region "IList"

    Private ReadOnly Property IList_Count() As Integer Implements ICollection.Count
        Get
            Return _List.Count
        End Get
    End Property

    Private Sub IList_CopyTo(ByVal array As Array, _
                             ByVal index As Integer) Implements ICollection.CopyTo
        Me.CopyTo(CType(array, HIDLinkCollectionNode()), index)
    End Sub

    Public ReadOnly Property IsSynchronized() As Boolean Implements ICollection.IsSynchronized
        Get
            Return True
        End Get
    End Property

    Public ReadOnly Property SyncRoot() As Object Implements ICollection.SyncRoot
        Get
            Return _LockObject
        End Get
    End Property

    Private Function IList_Add(ByVal value As Object) As Integer Implements IList.Add
        SyncLock Me.SyncRoot
            Me.Add(CType(value, HIDLinkCollectionNode))
            Return IndexOf(CType(value, HIDLinkCollectionNode))
        End SyncLock
    End Function

    Private Sub IList_Clear() Implements IList.Clear
        Me.Clear()
    End Sub

    Private Function IList_Contains(ByVal value As Object) As Boolean Implements IList.Contains
        Return Me.Contains(CType(value, HIDLinkCollectionNode))
    End Function

    Private Function IList_IndexOf(ByVal value As Object) As Integer Implements IList.IndexOf
        Return Me.IndexOf(CType(value, HIDLinkCollectionNode))
    End Function

    Private Sub IList_Insert(ByVal index As Integer, _
                             ByVal value As Object) Implements IList.Insert
        Me.Insert(index, CType(value, HIDLinkCollectionNode))
    End Sub

    Public ReadOnly Property IList_IsFixedSize() As Boolean Implements IList.IsFixedSize
        Get
            Return False
        End Get
    End Property

    Private ReadOnly Property IList_IsReadOnly() As Boolean Implements IList.IsReadOnly
        Get
            Return Me.IsReadOnly
        End Get
    End Property

    Private Property IList_Item(ByVal index As Integer) As Object Implements IList.Item
        Get
            Return CType(Me.Item(index), HIDLinkCollectionNode)
        End Get
        Set(ByVal value As Object)
            SyncLock Me.SyncRoot
                Me.Item(index) = CType(value, HIDLinkCollectionNode)
            End SyncLock
        End Set
    End Property

    Private Sub IList_Remove(ByVal value As Object) Implements IList.Remove
        Me.Remove(CType(value, HIDLinkCollectionNode))
    End Sub

    Private Sub IList_RemoveAt(ByVal index As Integer) Implements IList.RemoveAt
        Me.RemoveAt(index)
    End Sub

#End Region

#Region "Class Enumerator"

    Private Class HIDLinkCollectionNodeEnumerator
        Implements IEnumerator(Of HIDLinkCollectionNode)
        Private _Enumerator As IEnumerator

        Public Sub New(ByVal enumerator As IEnumerator)
            _Enumerator = enumerator
        End Sub

        Public ReadOnly Property Current() As HIDLinkCollectionNode Implements IEnumerator(Of HIDLinkCollectionNode).Current
            Get
                Return CType(_Enumerator.Current, HIDLinkCollectionNode)
            End Get
        End Property

        Private ReadOnly Property Current1() As Object Implements IEnumerator.Current
            Get
                Return Me.Current
            End Get
        End Property

        Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
            Return _Enumerator.MoveNext
        End Function

        Public Sub Reset() Implements IEnumerator.Reset
            _Enumerator.Reset()
        End Sub

#Region " IDisposable Support "
        Private disposedValue As Boolean = False
        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    _Enumerator = Nothing
                End If
            End If
            Me.disposedValue = True
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

#End Region
    End Class

#End Region

#Region " IDisposable Support "
    Private disposedValue As Boolean = False
    ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                _List.Clear()
                _List = Nothing
            End If

        End If
        Me.disposedValue = True
    End Sub

    Friend Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

#End Region
End Class

